﻿package mortal.game.proxy
{
    import Framework.Util.*;
    import Message.Game.*;
    import com.gengine.debug.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.common.error.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class MailProxy extends Proxy
    {
        private var _mailCache:MailCache;
        private var _hasAttachMent:Boolean;

        public function MailProxy()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._mailCache = this.cache.mail;
            return;
        }// end function

        public function queryMail(param1:int = 0, param2:int = 0, param3:int = 0, param4:int = 0) : void
        {
            rmi.iMailPrx.queryMail_async(new AMI_IMail_queryMail(this.querMailSucced, this.querMailFail), param1, param2, param3, param4);
            return;
        }// end function

        private function querMailSucced(param1:AMI_IMail_queryMail, param2:Array, param3:int, param4:int) : void
        {
            Log.debug("查询邮件成功");
            cache.mail.updateMails(param2, param3, param4);
            NetDispatcher.dispatchCmd(ServerCommand.MailListUpdate, null);
            return;
        }// end function

        private function querMailFail(param1:Exception) : void
        {
            Log.debug("查询邮件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function deleteMail(param1:Array, param2:Boolean = false) : void
        {
            this._hasAttachMent = param2;
            rmi.iMailPrx.removeMail_async(new AMI_IMail_removeMail(this.removeMailSucced, this.removeMailFail, param1), param1);
            return;
        }// end function

        private function removeMailSucced(param1:AMI_IMail_removeMail) : void
        {
            Log.debug("删除邮件成功");
            this._mailCache.removeMails(param1.userObject as Array);
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailDelteSucced));
            if (this._hasAttachMent)
            {
                MsgManager.showRollTipsMsg(Language.getString(20581));
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(20582));
            }
            return;
        }// end function

        private function removeMailFail(param1:Exception) : void
        {
            Log.debug("删除邮件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function readMail(param1:Number) : void
        {
            rmi.iMailPrx.readMail_async(new AMI_IMail_readMail(this.readMailSucced, this.readMailFail), param1);
            return;
        }// end function

        private function readMailSucced(param1:AMI_IMail_readMail, param2:SMail) : void
        {
            Log.debug("阅读邮件成功");
            this._mailCache.updateMail(param2);
            NetDispatcher.dispatchCmd(ServerCommand.MailUpdate, param2);
            return;
        }// end function

        private function readMailFail(param1:Exception) : void
        {
            Log.debug("阅读邮件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function sendMail(param1:String, param2:String, param3:String) : void
        {
            rmi.iMailPrx.sendMail_async(new AMI_IMail_sendMail(this.sendMailSucced, this.sendMailFail), param1, param2, param3, 0, 0, null);
            return;
        }// end function

        private function sendMailSucced(param1:AMI_IMail_sendMail) : void
        {
            Log.debug("发送邮件成功");
            MsgManager.showRollTipsMsg(Language.getString(20583));
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailSendSucced));
            return;
        }// end function

        private function sendMailFail(param1:Exception) : void
        {
            Log.debug("发送邮件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function getMailAttachment(param1:Number, param2:int, param3:Boolean) : void
        {
            rmi.iMailPrx.getMailAttachment_async(new AMI_IMail_getMailAttachment(this.getAttachSucced, this.getAttachFail), param1, param2, param3);
            return;
        }// end function

        private function getAttachSucced(param1:AMI_IMail_getMailAttachment, param2:Array) : void
        {
            var e:* = param1;
            var outMail:* = param2;
            Log.debug("获取附件成功");
            var mail:* = outMail[0];
            if (this._mailCache.mailGetAfterDelete && cache.mail.checkMailhadAttach(mail) == false)
            {
                var func:* = function () : void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailDelteSucced));
                return;
            }// end function
            ;
                this._mailCache.removeMails([outMail[0].mailId]);
                setTimeout(func, 100);
            }
            else
            {
                this._mailCache.updateMail(outMail[0]);
                NetDispatcher.dispatchCmd(ServerCommand.MailUpdate, outMail[0]);
            }
            return;
        }// end function

        private function getAttachFail(param1:Exception) : void
        {
            Log.debug("获取附件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function getMailAttachments_async(param1:Array) : void
        {
            rmi.iMailPrx.getMailAttachments_async(new AMI_IMail_getMailAttachments(this.getAttachsSucced, this.getAttachsFail), null);
            return;
        }// end function

        private function getAttachsSucced(param1:AMI_IMail_getMailAttachments, param2:Boolean, param3:Array) : void
        {
            var func:Function;
            var e:* = param1;
            var result:* = param2;
            var outMails:* = param3;
            func = function () : void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailGASucced));
                return;
            }// end function
            ;
            if (result == false)
            {
                MsgManager.showRollTipsMsg(Language.getString(20584));
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(20585));
                Log.debug("获取附件成功");
            }
            if (outMails.length == 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(20586));
                return;
            }
            if (this._mailCache.mailGetAfterDelete)
            {
                this.deleteMail(outMails);
            }
            else
            {
                setTimeout(func, 100);
            }
            return;
        }// end function

        private function getAttachsFail(param1:Exception) : void
        {
            Log.debug("获取附件失败");
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

    }
}
